package henu.graduation_design.onlineshop;

import java.util.HashMap;
import java.util.Map;

import henu.graduation_design.util.JsonLoader;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

public class ProductAppraiseActivity extends Activity {
	
	private int productID;
	private int orderID;
	SharedPreferences sp;
	private JsonLoader jsonLoader = new JsonLoader();
	
	private Button submitButton;
	private EditText contentEditText;
	private RatingBar ratingBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_product_appraise);
		Intent intent = getIntent();// 获得传过来的Intent。
		productID = intent.getIntExtra("prodcutID", 0);// 根据键name取出值。
		orderID = intent.getIntExtra("orderID", 0);
		submitButton=(Button)findViewById(R.id.button1);
		contentEditText=(EditText)findViewById(R.id.editText1);
		ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
		sp = getSharedPreferences("SP", MODE_PRIVATE);
		
		submitButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Map<String,Object> object = new HashMap<String, Object>();
				object.put("productID", productID);
				object.put("orderID", orderID);
				object.put("star", ratingBar.getRating());
				object.put("comment", contentEditText.getText().toString());
				loadJson(getString(R.string.url_send_appraise),
						sp,
						object);
			}
		});
	}
	
	private void loadJson(final String url, SharedPreferences spp,
			final Map<String,Object> mapEntry) {
		jsonLoader.postJson(url, spp, mapEntry,
				new JsonLoader.JsonObjectCallback() {
					public void jsonLoaded(JSONObject jsonObject) {
						boolean result = false;
						try {
							result = jsonObject
									.getBoolean("ProductAppraiseResult");
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if (result) {
							Toast.makeText(getApplicationContext(),
									"评价成功", Toast.LENGTH_LONG).show();
							finish();
						} else {
							Toast.makeText(getApplicationContext(), "评价失败",
									Toast.LENGTH_LONG).show();
						}
					}
				});
	}                    
}
